package com.demo.lab.service.impl;

import com.demo.lab.entity.Menu;
import com.demo.lab.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.demo.lab.mapper.UserMapper;
import com.demo.lab.entity.base.PageDomain;
import com.demo.lab.service.UserService;
import com.demo.lab.utils.MenuUtil;
import com.demo.lab.utils.ServletUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lgw
 * @since 2021-04-16
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;


    @Override
    public boolean checkUserLock(String username) {
        try {
            User user = userMapper.selectUserByName(username);
            return user.getUserLock().equals("0");
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean checkUserPwd(User user) {
        if (userMapper.selectUserByName(user.getUsername()) == null) {
            return false;
        }
        if (user.getUserpwd().equals(userMapper.selectUserByName(user.getUsername()).getUserpwd())) {
            //密码正确
            return true;
        }
        return false;
    }

    @Override
    public User findOne(User user) {
        //获取用户权限等级
        return userMapper.selectUser(user);
    }

    @Override
    public User findOne(String username) {
        return userMapper.selectUserByName(username);
    }

    @Override
    public User findOneById(String id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public List<Menu> getMenuListToAdmin() {
        List<Menu> menuList = userMapper.getMenuListToAdmin();
        MenuUtil mt = new MenuUtil(menuList);
        menuList = mt.builTree();
        return menuList;
    }

    @Override
    public List<Menu> getMenuListToUser() {
        List<Menu> menuList = userMapper.getMenuListToStu();
        MenuUtil mt = new MenuUtil(menuList);
        menuList = mt.builTree();
        return menuList;
    }

    @Override
    public List<Menu> getMenuListToTeacher() {
        List<Menu> menuList = userMapper.getMenuListToTeacher();
        MenuUtil mt = new MenuUtil(menuList);
        menuList = mt.builTree();
        return menuList;
    }

    @Override
    public boolean update(User user) {
        Integer result = userMapper.update(user);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean save(User user) {
        Integer result = userMapper.insert(user);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public PageInfo<User> page(User param, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        List<User> sysUsers = userMapper.selectList(param);
        //从分页列表中移除自己
        User thisUser = (User) ServletUtil.getSession().getAttribute("user");
        Iterator<User> it = sysUsers.iterator();
        while (it.hasNext()) {
            User userObj = it.next();
            if (userObj.getId().equals(thisUser.getId())) {
                it.remove();
            }
        }
        return new PageInfo<>(sysUsers);
    }

    @Override
    public boolean delete(String id) {
        User user = new User();
        user.setId(id);
        user.setDeleted("1");
        Integer result = userMapper.update(user);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean delete(String[] ids) {
        Integer result = userMapper.updateByIds(ids);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<User> lastLoginUser() {
        return userMapper.lastLoginUser();
    }
}
